//
//  MeViewController.swift
//  FM
//
//  Created by x on 2017/8/23.
//  Copyright © 2017年 HLB. All rights reserved.
//

import UIKit

class MeViewController: UIViewController {

    let titles:[String] = ["我的收藏","清理缓存","关于聆听夜空FM","设置"];
    let images:[String] = ["my_favorite","my_reccode","my_about","seting"]
    
    
    override func viewDidLoad() {
        super.viewDidLoad()

        setupUI()
    }
    
    /// 初始化
    func setupUI() -> Void {
        view.backgroundColor = UIColor.white
        navigationItem.title = "我的"
        
        view.addSubview(tableView)
    }
    
    //MARK: -- 懒加载
    private lazy var tableView:UITableView = {
        let tableView = UITableView()
        tableView.frame = CGRect(x: 0, y: 0, width: self.view.bounds.width, height: self.view.bounds.height-64-44)
        tableView.delegate = self
        tableView.dataSource = self
        tableView.separatorStyle = UITableViewCellSeparatorStyle.none
        tableView.tableFooterView = UIView(frame: CGRect.zero)
        tableView.bounces = false
        //注册cell
        tableView.register(LoginCell.self, forCellReuseIdentifier: loginCellIdentifier)
        tableView.register(UserinfoCell.self, forCellReuseIdentifier: userInfoCellIdentifier)
        
        return tableView
    }()
}

/// 登入cellID
private let loginCellIdentifier = "loginCellIdentifier"

/// 个人信息cell
private let userInfoCellIdentifier = "userInfoCellIdentifier"

extension MeViewController:UITableViewDelegate,UITableViewDataSource {

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 5
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        if indexPath.row == 0 {
            let cell = tableView.dequeueReusableCell(withIdentifier: loginCellIdentifier) as! LoginCell
            
            return cell
        }else {
            let cell = tableView.dequeueReusableCell(withIdentifier: userInfoCellIdentifier) as! UserinfoCell
            cell.title = titles[indexPath.row-1]
            cell.imageName = images[indexPath.row-1]
            
            return cell
        }
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
        
        //1.跳转VC
        let loginVC = LoginViewController()
        let navi = UINavigationController(rootViewController: loginVC)
        present(navi, animated: true, completion: nil)
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        if indexPath.row == 0 {
            return 85
        }
        return 50
    }
}

// MARK: -- 定义登入cell

class LoginCell: UITableViewCell {
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        //初始化
        setupUI()
    }
    
    func setupUI() -> Void {
        
        //1.添加子控件
        contentView.addSubview(iconHeadImage)
        contentView.addSubview(nameTitle)
        contentView.addSubview(markImage)
        contentView.addSubview(bgLbale)
        
        //2.布局子控件
        let dict = ["iconHeadImage":iconHeadImage,"nameTitle":nameTitle,"markImage":markImage,"bgLbale":bgLbale] as [String:Any]
        
        var cons:[NSLayoutConstraint] = [NSLayoutConstraint]()
        
        cons += NSLayoutConstraint.constraints(withVisualFormat: "H:|-20-[iconHeadImage(50)]", options: NSLayoutFormatOptions.init(rawValue: 0), metrics: nil, views: dict)
        cons += NSLayoutConstraint.constraints(withVisualFormat: "V:|-15-[iconHeadImage(50)]", options: NSLayoutFormatOptions.init(rawValue: 0), metrics: nil, views: dict)
        
        cons += NSLayoutConstraint.constraints(withVisualFormat: "H:[iconHeadImage]-30-[nameTitle]", options: NSLayoutFormatOptions.alignAllCenterY, metrics: nil, views: dict)
        
        cons += NSLayoutConstraint.constraints(withVisualFormat: "H:[markImage(15)]-20-|", options: NSLayoutFormatOptions.init(rawValue: 0), metrics: nil, views: dict)
        cons += NSLayoutConstraint.constraints(withVisualFormat: "V:|-32-[markImage(15)]", options: NSLayoutFormatOptions.init(rawValue: 0), metrics: nil, views: dict)
        
        cons += NSLayoutConstraint.constraints(withVisualFormat: "H:|-0-[bgLbale]-0-|", options: NSLayoutFormatOptions.init(rawValue: 0), metrics: nil, views: dict)
        cons += NSLayoutConstraint.constraints(withVisualFormat: "V:[bgLbale(5)]-0-|", options: NSLayoutFormatOptions.init(rawValue: 0), metrics: nil, views: dict)
        
        contentView.addConstraints(cons)
    }
    
    //MARK: -- 懒加载
    
    private lazy var iconHeadImage:UIImageView = UIImageView.imageWith(radios: 25, imageName: "my_user")
    
    private lazy var nameTitle:UILabel = UILabel.lableWith(title: "登入", titleColor: UIColor.black, fontSize: 16)
    
    private lazy var markImage:UIImageView = UIImageView.imageWith(radios: 0, imageName: "my_right_jump")
    
    private lazy var bgLbale:UILabel = UILabel.lableWith(backgroundColor: UIColor(white: 0.9, alpha: 0.9))
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

//MARK: -- 定义个人信息cell

class UserinfoCell: UITableViewCell {
    
    var title:String? {
        didSet {
            nameTitle.text = title
        }
    }
    
    var imageName:String? {
        didSet {
            guard imageName != nil else {
                return
            }
            iconHeadImage.image = UIImage.init(named: imageName!)
        }
    }
    
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    /// 初始化
    func setupUI() -> Void {
        
        //1.添加子控件
        contentView.addSubview(iconHeadImage)
        contentView.addSubview(nameTitle)
        contentView.addSubview(markImage)
        contentView.addSubview(bgLbale)
        
        //2.布局子控件
        let dict = ["iconHeadImage":iconHeadImage,"nameTitle":nameTitle,"markImage":markImage,"bgLbale":bgLbale] as [String:Any]
        
        var cons:[NSLayoutConstraint] = [NSLayoutConstraint]()
        
        cons += NSLayoutConstraint.constraints(withVisualFormat: "H:|-10-[iconHeadImage(20)]", options: NSLayoutFormatOptions.init(rawValue: 0), metrics: nil, views: dict)
        cons += NSLayoutConstraint.constraints(withVisualFormat: "V:|-7-[iconHeadImage(20)]", options: NSLayoutFormatOptions.init(rawValue: 0), metrics: nil, views: dict)
        
        cons += NSLayoutConstraint.constraints(withVisualFormat: "H:[iconHeadImage]-20-[nameTitle]", options: NSLayoutFormatOptions.alignAllCenterY, metrics: nil, views: dict)
        
        cons += NSLayoutConstraint.constraints(withVisualFormat: "H:[markImage(15)]-20-|", options: NSLayoutFormatOptions.init(rawValue: 0), metrics: nil, views: dict)
        cons += NSLayoutConstraint.constraints(withVisualFormat: "V:|-15-[markImage(15)]", options: NSLayoutFormatOptions.init(rawValue: 0), metrics: nil, views: dict)
        
        cons += NSLayoutConstraint.constraints(withVisualFormat: "H:|-0-[bgLbale]-0-|", options: NSLayoutFormatOptions.init(rawValue: 0), metrics: nil, views: dict)
        cons += NSLayoutConstraint.constraints(withVisualFormat: "V:[bgLbale(5)]-0-|", options: NSLayoutFormatOptions.init(rawValue: 0), metrics: nil, views: dict)
        
        contentView.addConstraints(cons)
    }
    //MARK: -- 懒加载
    
    private lazy var iconHeadImage:UIImageView = UIImageView.layoutImageView()
    
    private lazy var nameTitle:UILabel = UILabel.lableWith(title: "", titleColor: UIColor.black, fontSize: 12)
    
    private lazy var markImage:UIImageView = UIImageView.imageWith(radios: 0, imageName: "my_right_jump")
        
    private lazy var bgLbale:UILabel = UILabel.lableWith(backgroundColor: UIColor(white: 0.9, alpha: 0.9))
    
}














